package com.bot.edu.informationmanage.stumiddlecls.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bot.edu.informationmanage.classStage.entity.ClassStage;
import com.bot.edu.informationmanage.classStage.service.IClassStageService;
import com.bot.edu.informationmanage.cls.entity.Cls;
import com.bot.edu.informationmanage.cls.service.IClsService;
import com.bot.edu.informationmanage.major.entity.Major;
import com.bot.edu.informationmanage.major.service.IMajorService;
import com.bot.edu.informationmanage.stu.entity.Stu;
import com.bot.edu.informationmanage.stumiddlecls.entity.StuMiddleCls;
import com.bot.edu.informationmanage.stumiddlecls.entity.vo.StuMiddleClsVo;
import com.bot.edu.noticessystem.notices.entity.Notices;
import com.bot.edu.noticessystem.notices.service.INoticesService;
import com.bot.edu.utils.ToNotices;
import org.apache.ibatis.annotations.Param;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;

import com.bot.edu.informationmanage.stumiddlecls.service.IStuMiddleClsService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 学生班级中间表
 * @Author: jeecg-boot
 * @Date:   2022-04-02
 * @Version: V1.0
 */
@Api(tags="学生班级中间表")
@RestController
@RequestMapping("/stumiddlecls/stuMiddleCls")
@Slf4j
public class StuMiddleClsController extends JeecgController<StuMiddleCls, IStuMiddleClsService> {
	@Autowired
	private IStuMiddleClsService stuMiddleClsService;

	@Autowired
    private IMajorService majorService;

	@Autowired
    private IClsService clsService;

	@Autowired
    private IClassStageService classStageService;

     @Autowired
     private INoticesService noticesService;
	/**
	 * 分页列表查询
	 *
	 * @param stuMiddleCls
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "学生班级中间表-分页列表查询")
	@ApiOperation(value="学生班级中间表-分页列表查询", notes="学生班级中间表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<StuMiddleCls>> queryPageList(StuMiddleCls stuMiddleCls,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<StuMiddleCls> queryWrapper = QueryGenerator.initQueryWrapper(stuMiddleCls, req.getParameterMap());
		Page<StuMiddleCls> page = new Page<StuMiddleCls>(pageNo, pageSize);
		IPage<StuMiddleCls> pageList = stuMiddleClsService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param stuMiddleCls
	 * @return
	 */
	@AutoLog(value = "学生班级中间表-添加")
	@ApiOperation(value="学生班级中间表-添加", notes="学生班级中间表-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody StuMiddleCls stuMiddleCls) {
		stuMiddleClsService.save(stuMiddleCls);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param stuMiddleCls
	 * @return
	 */
	@AutoLog(value = "学生班级中间表-编辑")
	@ApiOperation(value="学生班级中间表-编辑", notes="学生班级中间表-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody StuMiddleCls stuMiddleCls) {
		stuMiddleClsService.updateById(stuMiddleCls);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "学生班级中间表-通过id删除")
	@ApiOperation(value="学生班级中间表-通过id删除", notes="学生班级中间表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		stuMiddleClsService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "学生班级中间表-批量删除")
	@ApiOperation(value="学生班级中间表-批量删除", notes="学生班级中间表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.stuMiddleClsService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "学生班级中间表-通过id查询")
	@ApiOperation(value="学生班级中间表-通过id查询", notes="学生班级中间表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<StuMiddleCls> queryById(@RequestParam(name="id",required=true) String id) {
		StuMiddleCls stuMiddleCls = stuMiddleClsService.getById(id);
		if(stuMiddleCls==null) {
			return Result.error("未找到对应数据",null);
		}
		return Result.OK(stuMiddleCls);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param stuMiddleCls
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, StuMiddleCls stuMiddleCls) {
        return super.exportXls(request, stuMiddleCls, StuMiddleCls.class, "学生班级中间表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, StuMiddleCls.class);
    }

	 /**
	  * 通过班级id查看班级学生
 	  * @param
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	@RequestMapping(value = "/Volist/{classId}/{stuName}",method = RequestMethod.POST)
	 public Result<IPage<StuMiddleClsVo>> Volist(@PathVariable("classId")String classId,
												 @PathVariable("stuName") String stuName,
												 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												 HttpServletRequest req){
			QueryWrapper<StuMiddleClsVo> wrapper = new QueryWrapper<>();
            if (classId!=null) {
            	wrapper.lambda().eq(StuMiddleClsVo::getClassId,classId);
            }
		    if (!stuName.equals("null")) {
			wrapper.like("stu_name",stuName);
		}
		    Page<StuMiddleClsVo> IPage= new Page<>(pageNo,pageSize);
		     stuMiddleClsService.pageVo(IPage,wrapper);
		    return Result.OK(IPage);
		}
	 /**
	  * 根据班级id得到学生信息
	  */
	 @RequestMapping(value = "/CheckClsName/{classId}",method = RequestMethod.GET)
	 public Result<?> CheckClsName(@PathVariable("classId") String classId){
		 List<StuMiddleClsVo> checkClsName=stuMiddleClsService.CheckClsName(classId);
		 return Result.OK(checkClsName);

	}

	 /**
	  * 获取选中班级外的学生
	  * @param classId
	  * @return
	  */
	 @RequestMapping(value = "/getStuList",method = RequestMethod.GET)
	 public Result<?> getStuList(String classId){
	 	List<StuMiddleClsVo> stuMiddleClsVoList = stuMiddleClsService.getStuList(classId);
	 	return Result.ok(stuMiddleClsVoList);
	 }

	 @RequestMapping(value = "/upStuListByCls",method = RequestMethod.POST)
	 public Result<?> upStuListByCls(@RequestBody List<String> targetKeys,String classId){
	 	//设置当前班（右）学生班级为0
		 UpdateWrapper<StuMiddleCls> stuMiddleClsUpdateWrapper = new UpdateWrapper<>();
		 stuMiddleClsUpdateWrapper.lambda().eq(StuMiddleCls::getClassId,classId)
				                           .set(StuMiddleCls::getClassId,0);
		 stuMiddleClsService.update(stuMiddleClsUpdateWrapper);

		 //根据前端传入的学生编号，并将其班级改为当前班级
		 UpdateWrapper<StuMiddleCls> stuMiddleClsUpdateWrapper2 = new UpdateWrapper<>();
		 stuMiddleClsUpdateWrapper2.lambda().in(StuMiddleCls::getStuId,targetKeys)
				                            .set(StuMiddleCls::getClassId,classId);
		 stuMiddleClsService.update(stuMiddleClsUpdateWrapper2);
         Notices notices = ToNotices.toNotices( "您的班级信息已变更，请在个人资料中查看");
         noticesService.save(notices);
		 return Result.ok();
	 }
	 /**
	  * 没有班级编号的学生
	  */
	 @RequestMapping(value = "/noClsIdStuList/{classId}",method = RequestMethod.GET)
	 public Result<List<StuMiddleCls>> noClsIdStuList(@PathVariable("classId") String classId){
           List<StuMiddleCls> noClsIdList=stuMiddleClsService.getNoClsId(classId);
           return Result.ok(noClsIdList);
	 }

     /**
      *  个人资料班级内容显示
      * @param stuId
      * @return
      */
	 @GetMapping("/getClsBystuId")
     public Result<?> getClsBystuId(String stuId){
         HashMap<Object, Object> clsDataHashMap = new HashMap<>();
	     /*查询中间表*/
         QueryWrapper<StuMiddleCls> stuMiddleClsQueryWrapper = new QueryWrapper<>();
         stuMiddleClsQueryWrapper.lambda().eq(StuMiddleCls::getStuId,stuId);
         StuMiddleCls cls = stuMiddleClsService.getOne(stuMiddleClsQueryWrapper);
         if(cls!=null){
             /*查询班级*/
             QueryWrapper<Cls> clsQueryWrapper = new QueryWrapper<>();
             clsQueryWrapper.lambda().eq(Cls::getClassId,cls.getClassId());
             Cls clsData = clsService.getOne(clsQueryWrapper);
             /*查询班级阶段*/
             QueryWrapper<ClassStage> classStageQueryWrapper = new QueryWrapper<>();
             classStageQueryWrapper.lambda().eq(ClassStage::getClassStageId,clsData.getClassStageId());
             ClassStage classStage = classStageService.getOne(classStageQueryWrapper);
             /*查询专业*/
             QueryWrapper<Major> majorQueryWrapper = new QueryWrapper<>();
             majorQueryWrapper.lambda().eq(Major::getMajorId,clsData.getMajorId());
             Major major = majorService.getOne(majorQueryWrapper);

             clsDataHashMap.put("clsData",clsData);
             clsDataHashMap.put("classStage",classStage);
             clsDataHashMap.put("major",major);
         }


         return Result.ok(clsDataHashMap);
     }
 }

